<template>
  <div class="app-container">
    <el-form
      ref="form"
      class="form"
      :rules="rules"
      :model="form"
      label-width="120px"
    >
      <el-form-item label="排序" prop="sortNo">
        <el-input
          v-model="form.sortNo"
          placeholder="每人限制使用张数，0无限制"
        ></el-input>
      </el-form-item>

      <el-form-item label="跳转链接">
        <el-input
          v-model="form.jumpUrl"
          placeholder="请输入跳转链接"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="图片"
        prop="imgUrl"
        :rules="[{ required: true, message: '请上传图片', trigger: 'change' }]"
      >
        <el-upload
          :action="`${BASE_URL}/common/images/upload`"
          accept="image/jpeg,image/jpg,image/png"
          list-type="picture-card"
          :class="{ disabled: uploadDisabled }"
          :file-list="pictureList"
          :on-remove="handleUploadChange"
          :on-change="handleUploadChange"
          :before-upload="beforeAvatarUpload"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          form.id ? "保存修改" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addOrEditBanner, getBannerDetail } from "@/api/mini/index";
import { BASE_URL, IMAGE_OSS_URL } from "@/config/index";

export default {
  data() {
    return {
      IMAGE_OSS_URL,
      BASE_URL,
      form: {
        sortNo: "",
        jumpUrl: "",
        imgUrl: "",
      },
      loading: false,
      pictureList: [],
      rules: {
        jumpUrl: [
          {
            pattern: /^http[s]?:\/\/.*/,
            message: "请输入正确的链接",
            trigger: "blur",
          },
        ],
        sortNo: [
          { required: true, message: "请输入限制张数", trigger: "blur" },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: "请输入正整数",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    uploadDisabled: function () {
      return this.form.imgUrl;
    },
  },
  created() {
    if (this.$route.query.id) {
      this.getDetail();
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 10MB!");
      }
      return isLt2M;
    },
    handleUploadChange(file, fileList) {
      this.pictureList = fileList;
      this.form.imgUrl = fileList.map((item) => item.url).join(",");
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          addOrEditBanner({
            ...this.form,
            imgUrl: this.pictureList
              .map((item) => item.response.data)
              .join(","),
          })
            .then((res) => {
              this.loading = false;
              this.$message.success("操作成功");
              this.$router.push("/mini/banner/list");
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getDetail() {
      getBannerDetail(this.$route.query.id).then((res) => {
        this.form = res.data;
        this.pictureList = this.form.imgUrl
          ? this.form.imgUrl.split(",").map((item) => ({
              url: IMAGE_OSS_URL + item,
              status: "success",
              response: {
                data: item,
              },
            }))
          : [];
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form {
  width: 480px;
}
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>
